import { observable, action } from 'mobx';
import EventBus from '@/misc/EventBus';

export default class DialogStore {
    @observable public displayDialog = false;
    @observable public showCancelButton?: boolean;
    @observable public text = '';
    @observable public title = '';
    @observable public confirmText = '';
    @observable public cancelText = '';

    @action
    public async showConfirm(text: string, title = 'Notice', confirmText = 'OK', cancelText = 'CANCEL'): Promise<boolean> {
        this.text = text;
        this.title = title;
        this.confirmText = confirmText;
        this.cancelText = cancelText;
        this.showCancelButton = true;
        this.displayDialog = true;
        const confirm = await new Promise<boolean>((resolve) => {
            EventBus.$on('dialog-event-confirm', (result: boolean) => {
                resolve(result);
            });
        });
        this.displayDialog = false;
        return confirm;
    }

    @action
    public async showWarning(text: string, title = 'Warning', confirmText = 'OK'): Promise<boolean> {
        this.text = text;
        this.title = title;
        this.confirmText = confirmText;
        this.showCancelButton = false;
        this.displayDialog = true;
        const confirm = await new Promise<boolean>((resolve) => {
            EventBus.$on('dialog-event-confirm', (result: boolean) => {
                resolve(result);
            });
        });
        this.displayDialog = false;
        return confirm;
    }
}
